package com.wangc.commons.config;

import com.wangc.commons.utils.PfRedisTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 类描述： redis配置 </br>
 *
 * @author liujie </br>
 * @date 2020/4/8 </br>
 */
@Configuration
public class RedisConfiguration {

    // 连接池最大连接数（使用负值表示没有限制）
    @Value("${spring.redis.pool.max-active:64}")
    private Integer maxTotal;
    // 连接池中的最大空闲连接
    @Value("${spring.redis.pool.max-idle:30}")
    private Integer maxIdle;
    // 连接池中的最小空闲连接
    @Value("${spring.redis.pool.min-idle:5}")
    private Integer minIdle;
    // 连接池最大阻塞等待时间（使用负值表示没有限制）
    @Value("${spring.redis.pool.max-wait:3000}")
    private Integer maxWaitMillis;

    @Bean(name = "longRedisTemplate")
    public RedisTemplate<String, Long> longRedisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate<String, Long> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericToStringSerializer<>(Long.class));
        template.setConnectionFactory(jedisConnectionFactory);
        return template;
    }

    @Bean(name = "floatRedisTemplate")
    public RedisTemplate<String, Float> floatRedisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate<String, Float> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Float.class));
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        return redisTemplate;
    }

    @Bean("redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        PfRedisTemplate<String, Object> customerTemplate = new PfRedisTemplate<>();
        customerTemplate.setConnectionFactory(jedisConnectionFactory);
        customerTemplate.setKeySerializer(new StringRedisSerializer());
        customerTemplate.setValueSerializer(new GenericToStringSerializer(Object.class));
        customerTemplate.setDefaultSerializer(new GenericToStringSerializer(Object.class));
        customerTemplate.setEnableDefaultSerializer(true);
        return customerTemplate;
    }
}
